package com.chuanye.mydrawable;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.chuanye.mydrawable.activitys.CreateRepairsActivity;
import com.chuanye.mydrawable.beans.LoginBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button button1;
    private Button button2;

    private Context mContext;

    /**
     * 新建报修RepairHandle 获取故障类型 GetAllFaultType    远程服务GetRemoteList
     * 现场服务GetFieldList 维修换新 GetReplaceInfoList
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);

        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);

        EditText et = (EditText) findViewById(R.id.et);
        et.setOnClickListener(this);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
        //btn.setClickable(false);
        btn.setEnabled(false);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1://新建报修
                Intent intent = new Intent(MainActivity.this, CreateRepairsActivity.class);
                startActivity(intent);
                break;

            case R.id.button2://登录
                login("dn","q123456");
                break;
            case R.id.et:
                Toast.makeText(mContext,"点击了",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn:
                Toast.makeText(mContext,"点击了",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void login(String username,String password) {
        String password1 = md5(password);
        Log.i("密码：",""+password1);//c51cd8e64b0aeb778364765013df9ebe
        OkHttpUtils.post()
                .url("http://172.16.1.121:9001/api/Login")//用户登陆并做与设备关系处理
                .addParams("UserName", username)//ZMX  LXQ
                .addParams("Password", password1)//q123456
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.i("登录请求的数据onError：",e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("登录请求的数据：",response.toString());
                        String str = response.toString();
                        SharedPreferences sharedPreferences = mContext.getSharedPreferences("User",MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("name",str);
                        editor.commit();




                    }
                });


    }


    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
